<template>
  <div class="modal">
    <!-- 遮罩层 -->
    <div class="mask" @click="changeIsShowModal(false)"></div>
    <!-- 登录窗口 -->
    <div class="modalView">
      <!-- 关闭按钮 -->
      <div class="close" @click="changeIsShowModal(false)"></div>
      <!-- 登录方式导航 -->
      <div class="nav">
        <a
          href="#"
          :class="{ active: isShowContent }"
          @click="isShowContent = true"
          >手机号登录</a
        >|<a href="#" :class="{ active: !isShowContent }" @click="showQrCode"
          >微信扫码登录</a
        >
      </div>
      <div class="content">
        <div class="phoneNumLogin" v-show="isShowContent">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              class="ipt"
              v-model="phoneNum"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
            ></slide-verify>
          </div>
          <div class="row">
            <input
              type="text"
              placeholder="请输入验证码"
              class="ipt"
              v-model="QRcodeMsg"
            />
            <button v-show="!isShowtime" class="btn" @click="getQRcode">
              获取验证码
            </button>
            <button v-show="isShowtime" class="btn">{{ count }}s</button>
          </div>
          <button class="loginBtn" @click="submitFn">登录</button>
        </div>
        <div class="QRcodeLogin" v-show="!isShowContent" id="weixin"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { validateTelephoneNumber } from "@/utils";
import { sendSMSAPI, phoneLoginAPI, bdPhoneAPI } from "@/request/api";

export default {
  data() {
    return {
      // 登录登录框显示
      isShowContent: true,
      // 倒计时按钮是否显示
      isShowtime: false,
      // 滑块信息
      msg: "向右滑动",
      // 手机号
      phoneNum: "",
      // 验证码
      QRcodeMsg: "",
      // 倒计时
      count: 5,
    };
  },
  computed: {
    ...mapState({
      // 控制模态窗显示隐藏
      isShowModal: (state) => state.showModal.isShowModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },
  methods: {
    ...mapMutations({
      // 控制模态窗显示隐藏
      changeIsShowModal: "showModal/changeIsShowModal",
      // 登录状态
      changeIsLogin: "showLogin/changeIsLogin",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 封装校验
    toVerity() {
      // 手机号非空校验
      if (!this.phoneNum) {
        this.asyncChangeIsShowToast({
          msg: "手机号不能为空",
          type: "danger",
        });
        console.log("不能为空");
        return;
      }
      // 手机号真假校验
      if (!validateTelephoneNumber(this.phoneNum)) {
        this.asyncChangeIsShowToast({
          msg: "输入正确手机号",
          type: "warning",
        });
        console.log("输入正确手机号");
        return;
      }
      // 滑块校验
      if (this.msg == "向右滑动" || this.msg == "再试一次") {
        this.asyncChangeIsShowToast({
          msg: "将滑块滑至正确位置",
          type: "danger",
        });
        console.log("滑至正确位置");
        return;
      }
      return true;
    },
    // 获取验证码
    async getQRcode() {
      // 校验
      // if (!this.toVerity()) return;
      // 倒计时开始
      var timer = null;
      this.isShowtime = true;
      timer = setInterval(() => {
        this.count--;
        if (this.count == 1) {
          clearInterval(timer);
          this.isShowtime = false;
        }
      }, 1000);
      // 异步请求
      // let res = await sendSMSAPI(this.phoneNum);
      // 提示
      this.asyncChangeIsShowToast({
        msg: "已发送手机注册验证信息",
        type: "success",
      });
      this.phoneNum = "";
    },

    // 点击登录
    async submitFn() {
      if (!this.QRcodeMsg) {
        this.asyncChangeIsShowToast({
          msg: "验证码不能为空",
          type: "danger",
        });
        return;
      }
      // 判定是否是绑定微信的登录 --  uuid
      // 获取uuid
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        // 发送绑定微信的请求
        res = await bdPhoneAPI({
          phone: this.phoneNum,
          verifyCode: this.QRcodeMsg,
          uuid: uuid,
        });
        // 更新登录界面
        this.changeIsLogin(true);
      } else {
        // 发起登录请求
        res = await phoneLoginAPI({
          verifyCode: this.QRcodeMsg,
          phone: this.phoneNum,
        });
      }
      if (!res) return;
      console.log(res);
      // 保存token值
      localStorage.setItem("x-auth-token", res["x-auth-token"]);
      // 提示登录成功
      this.asyncChangeIsShowToast({
        msg: "登录成功",
        type: "success",
      });
      // 隐藏模态框
      this.changeIsShowModal(false);
      // 更新登录界面
      this.changeIsLogin(true);
      // 删除uuid
      localStorage.removeItem("uuid");
      // 清除code值
      this.$router.push(this.$route.path);
    },

    // 显示二维码
    showQrCode() {
      this.isShowContent = false;
      //获取二维码 并展示
      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLA0KLmltcG93ZXJCb3ggLmluZm8gew0KICBkaXNwbGF5OiBub25lOw0KfQ0K",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.modal {
  width: 100%;
  height: 100%;
  // background-color: skyblue;
  position: fixed;
  top: 0;
  left: 0;
  div.mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
  div.modalView {
    width: 555px;
    height: 423px;
    // background-color: skyblue;
    background: url("../assets/img/login-box-bg.png");
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    div.close {
      width: 22px;
      height: 22px;
      background: url("../assets/img/close.png");
      position: absolute;
      top: 15px;
      right: 60px;
      cursor: pointer;
    }
    .nav {
      padding-top: 50px;
      a {
        font-size: 20px;
        padding: 0 10px;
        text-decoration: none;
        color: #aaa;
        &.active {
          color: rgb(24, 18, 18);
        }
      }
    }
    div.content {
      width: 355px;
      margin: 20px auto 0;
      height: 200px;
      .phoneNumLogin {
        .row {
          display: flex;
          margin-bottom: 25px;
          .ipt {
            flex: 1;
            outline: none;
            height: 40px;
            padding-left: 9px;
          }
          .btn {
            width: 77px;
            margin-left: 5px;
            background: #295ace;
            padding: 0 3px;
            color: #fff;
            outline: none;
          }
        }
        .loginBtn {
          width: 100%;
          height: 40px;
          background: #295ace;
          color: #fff;
          font-size: 16px;
          outline: none;
        }
      }
      #weixin {
        /* background-color: #fcf; */
        display: flex;
        justify-content: center;
        margin-top: -20px;
      }
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>
